//
//  PhoneNumberView.m
//  iShanggang
//
//  Created by aiShangGang on 2017/4/14.
//  Copyright © 2017年 aishanggang. All rights reserved.
//

#import "PhoneNumberView.h"
#import "BOHUDManager.h"
#import "UIColor+Hex.h"
@interface PhoneNumberView ()

@property (nonatomic,weak)UILabel *accontLbl;
@property (nonatomic,weak)UILabel *passwordLbl;
@property (nonatomic,weak)UIButton *eyeBtn;
@property (nonatomic,weak)UIView *accountView;
@property (nonatomic,weak)UIView *passwordView;
@property (nonatomic,weak)UIView *longLineView;
@property (nonatomic,weak)UIButton *loginBtn;
@property (nonatomic,weak)UIButton *forgetPasswordBtn;
@property (nonatomic,weak)UIButton *redomRunBtn;
@property (nonatomic,weak)UIButton *protocalBtn;
@end

@implementation PhoneNumberView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        
        UIView *accountV = [[UIView alloc]init];
        accountV.backgroundColor = [UIColor whiteColor];
        [self addSubview:accountV];
        self.accountView = accountV;
        
        UILabel *accountLbl = [self titleText:@"手机号" view:accountV];
        self.accontLbl = accountLbl;
        UITextField *accountTextField = [self placeHolder:@"请输入手机号码" view:accountV];
        accountTextField.tag = 1;
        self.phoneField = accountTextField;
        
        UIView *passwordV = [[UIView alloc]init];
        passwordV.backgroundColor = [UIColor whiteColor];
        [self addSubview:passwordV];
        self.passwordView = passwordV;
        
        UILabel *passwordLbl = [self titleText:@"验证码" view:passwordV];
        self.passwordLbl = passwordLbl;
        
        UITextField *passwordTextField = [self placeHolder:@"请输入验证码" view:passwordV];
        passwordTextField.tag = 2;
        self.codeField = passwordTextField;
        
        UIButton *loginBtn = [[UIButton alloc]init];
        loginBtn.backgroundColor = [UIColor colorWithHex:0xC4C4C4];
        [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
        [loginBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        loginBtn.layer.cornerRadius = 5;
        loginBtn.layer.masksToBounds = YES;
        loginBtn.userInteractionEnabled = NO;
        [loginBtn addTarget:self action:@selector(didClickLoginBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:loginBtn];
        self.loginBtn = loginBtn;
        
        UIButton *eyeBtn = [[UIButton alloc]init];
        [eyeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        eyeBtn.titleLabel.font = [UIFont systemFontOfSize:14];
        [eyeBtn setTitleColor:cCommonRedColor forState:UIControlStateNormal];
        [eyeBtn addTarget:self action:@selector(didClickEyeBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:eyeBtn];
        self.eyeBtn = eyeBtn;
        
        UIView *longLine = [[UIView alloc]init];
        longLine.backgroundColor = cCommonLineColor;
        [self addSubview:longLine];
        self.longLineView = longLine;
    }
    return self;
}

- (UIButton *)title:(NSString *)title color:(long)color {
    UIButton *button = [[UIButton alloc]init];
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:16];
    [button setTitleColor:[UIColor colorWithHex:color] forState:UIControlStateNormal];
    [self addSubview:button];
    return button;
}

- (UILabel *)titleText:(NSString *)titleText view:(UIView *)View {
    UILabel *label = [[UILabel alloc]init];
    label.text = titleText;
    label.textColor = [UIColor colorWithHex:0x333333];
    label.textAlignment = NSTextAlignmentLeft;
    label.font = [UIFont systemFontOfSize:15];
    [View addSubview:label];
    return label;
}

- (UITextField *)placeHolder:(NSString *)placeHolder view:(UIView *)passwordV {
    UITextField *textFiled = [[UITextField alloc]init];
    textFiled.placeholder = placeHolder;
    [textFiled setValue:[UIColor colorWithHex:0x999999] forKeyPath:@"_placeholderLabel.textColor"];
    textFiled.textColor = [UIColor colorWithHex:0x333333];
    textFiled.font = [UIFont systemFontOfSize:15];
    textFiled.keyboardType = UIKeyboardTypeNumberPad;
    [textFiled addTarget:self action:@selector(chengeBtnState) forControlEvents:UIControlEventEditingChanged];
    [passwordV addSubview:textFiled];
    return textFiled;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    [self.accountView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@10);
        make.left.right.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [self.longLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(15);
        make.right.equalTo(self);
        make.top.equalTo(self.accountView.mas_bottom);
        make.height.equalTo(@1);
    }];
    
    [self.passwordView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.accountView.mas_bottom);
        make.left.right.equalTo(@0);
        make.height.equalTo(@44);
    }];
    
    [self.accontLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accountView).offset(15);
        make.top.equalTo(self.accountView);
        make.height.equalTo(@44);
    }];
    
    [self.phoneField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.accontLbl.mas_right).offset(30);
        make.top.bottom.equalTo(self.accountView);
        make.width.equalTo(@(Width - 135));
    }];
    
    [self.passwordLbl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.accontLbl);
        make.top.bottom.equalTo(self.passwordView);
    }];
    
    [self.eyeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.passwordView.mas_right).offset(-20);
        make.centerY.equalTo(self.passwordView.mas_centerY);
    }];
    
    [self.codeField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.phoneField);
        make.top.bottom.equalTo(self.passwordView);
        make.width.equalTo(@(Width - 165));
    }];
    
    [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.passwordView.mas_bottom).offset(30);
        make.height.equalTo(@44);
        make.leading.equalTo(self.passwordLbl);
        make.right.equalTo(self.mas_right).offset(- 15);
    }];
}

#pragma mark - button target
- (void)didClickLoginBtn:(UIButton *)sender {
//    if (![BOAssistor phoneNumberIsValid:self.phoneField.text]) {
//        [BONoticeBar defaultBar].noticeText = @"请输入正确的手机号";
//    }else if (self.codeField.text.length != 4){
//        [BONoticeBar defaultBar].noticeText = @"请输入4位手机验证码";
//    }else{
//        [[NSNotificationCenter defaultCenter]postNotificationName:@"didClickLoginBtn" object:nil];
//    }
}

#pragma mark - 获取验证码
- (void)didClickEyeBtn:(UIButton *)sender {
//    if (self.phoneField.text.length > 0) {
//        if (![BOAssistor phoneNumberIsValid:self.phoneField.text]) {
//            [BONoticeBar defaultBar].noticeText = @"请输入正确的手机号";
//        }else{
//            [self getSessionID];
//        }
//    }else{
//        [BONoticeBar defaultBar].noticeText = @"请输入手机号";
//    }
}

- (void)getSessionID{
//    @weakify(self);
//    NSDictionary *pramas = [NSDictionary dictionaryWithObjectsAndKeys:self.phoneField.text,@"mobile",nil];
//    [[BOHUDManager defaultManager] progressHUDShowWithText:@"获取验证码"];
//    [ASGNetWorkEngine requestWithURL:getCodeURL sessionID:NO params:pramas completeBlock:^(NSDictionary *result) {
//        @strongify(self);
//        [[BOHUDManager defaultManager] progressHUDHideImmediately];
//
//        if ([result[@"status"]integerValue] == 0) {
//            [[BONoticeBar defaultBar] setNoticeText:@"获取验证码成功"];
//            [self timeChange];
//        }else{
//            [[BONoticeBar defaultBar] setNoticeText:result[@"msg"]];
//        }
//    } failedBlock:^(NSError *error) {
//        [[BOHUDManager defaultManager] progressHUDHideImmediately];
//    }];
}

- (void)timeChange{
}

#pragma mark - textfield delegate
- (void)chengeBtnState {
    
    if (self.phoneField.text.length>0 && self.codeField.text.length>0) {
        self.loginBtn.backgroundColor = [UIColor colorWithHex:0xd23023];
        self.loginBtn.userInteractionEnabled = YES;
    }else {
        self.loginBtn.backgroundColor = [UIColor colorWithHex:0xC4C4C4];
        self.loginBtn.userInteractionEnabled = NO;
    }
}

@end
